//
//  HEHomeHealthCheckCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2021/5/24.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HEHomeHealthCheckCell.h"
#import "HEMoreView.h"
#import "HEItemBtnView.h"

@interface HEHomeHealthCheckCell ()

@property (nonatomic, strong) UIScrollView *scrollView;
// 约束参照视图,也是容器视图
@property (nonatomic, strong) UIView *contentWarp;

@end

@implementation HEHomeHealthCheckCell
 
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initUI];
    }
    return self;
}

- (void)initUI {
    UIView *healthManage = [[UIView alloc] init];
    healthManage.backgroundColor = [UIColor whiteColor];
    healthManage.layer.shadowColor = [UIColor colorWithRed:13/255.0 green:98/255.0 blue:218/255.0 alpha:0.08].CGColor;
    healthManage.layer.shadowOffset = CGSizeMake(0,5);
    healthManage.layer.shadowOpacity = 1;
    healthManage.layer.shadowRadius = 6;
    healthManage.layer.cornerRadius = 10;
    [self.contentView addSubview:healthManage];
    
    // 健康管理
    HEMoreView *allView = [[HEMoreView alloc] init];
    allView.title = @"健康管理";
    [healthManage addSubview:allView];
    UITapGestureRecognizer *healthMngTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toHealthMangeClick)];
    [allView addGestureRecognizer:healthMngTap];
    
    [healthManage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(0);
        make.left.equalTo(self.contentView).mas_offset(0);
        make.right.equalTo(self.contentView).mas_offset(-0);
        make.bottom.equalTo(self.contentView).mas_offset(-0);
    }];
    
    [allView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(healthManage).mas_offset(15);
        make.left.equalTo(healthManage).mas_offset(15);
        make.right.equalTo(healthManage).mas_offset(-15);
    }];
    
    //
    // 1. 添加scrollView
    [healthManage addSubview:self.scrollView];
    // 2. 添加参照视图
    [self.scrollView addSubview:self.contentWarp];
    
    // 设置scrollView约束
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(allView.mas_bottom).mas_offset(0);
        make.left.equalTo(healthManage).mas_offset(15);
        make.right.equalTo(healthManage).mas_offset(-15);
        make.height.mas_equalTo(90);
        make.bottom.equalTo(healthManage).mas_offset(-0);
    }];
    
    // 设置参照视图的约束
    [self.contentWarp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.height.equalTo(self.scrollView); //水平滚动高度固定，这个很重要
    }];
    
  
    NSArray *manImgList = @[@{@"name":@"身高", @"icon":@"home_tall"},
                             @{@"name":@"体重", @"icon":@"home_weight"},
                             @{@"name":@"体温", @"icon":@"home_temperature"},
                             @{@"name":@"呼吸次数", @"icon":@"home_breathe"},
                             @{@"name":@"睡眠", @"icon":@"home_sleep"},
                             @{@"name":@"血压", @"icon":@"home_blood_pressure"},
                             @{@"name":@"血糖", @"icon":@"home_blood_glucose"},
                             @{@"name":@"血氧", @"icon":@"home_blood_o2"},
                             @{@"name":@"静息心率", @"icon":@"home_heart_rate"},
                             @{@"name":@"BMI", @"icon":@"home_bmi"}];
    NSMutableArray *manArr = [NSMutableArray array];

    for (int i = 0; i < manImgList.count; i++) {

        NSDictionary *img = manImgList[i];

        HEItemBtnView *sysBtn = [HEItemBtnView new];
        sysBtn.title = img[@"name"];
        sysBtn.imageName = img[@"icon"];
        sysBtn.titleColor = CNavLightBlackColor;
        sysBtn.titleFont = DYBlodFont(16);
        [sysBtn resetLayoutWithWidth:35 height:35];
        sysBtn.selectBlock = ^(HEItemBtnView * _Nonnull btn) {
            [self healthChcekClick: i];
        };
        [_contentWarp addSubview: sysBtn];

        [manArr addObject: sysBtn];
    }

    // 实现masonry水平固定间隔方法
    [manArr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:10 leadSpacing:0 tailSpacing:0];

    [manArr mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_contentWarp).mas_offset(15);
        make.bottom.mas_equalTo(_contentWarp).mas_offset(-15);
    }];
}

- (void)configCellWithFiterModel:(GDFiterCellModel *)fiterModel indexPath:(NSIndexPath *)indexPath {
    _delegate = self.proxyVC;

}

- (void)toHealthMangeClick {
    if ([_delegate respondsToSelector:@selector(healthChcekCellAllHandler)]) {
        [_delegate healthChcekCellAllHandler];
    }
}

- (void)healthChcekClick:(NSInteger)index {
    if ([_delegate respondsToSelector:@selector(healthChcekCellTapHandler:)]) {
        [_delegate healthChcekCellTapHandler: index];
    }
}

#pragma mark - getters
// scrollView
- (UIScrollView *)scrollView {
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.backgroundColor = KWhiteColor;
    }
    return _scrollView;
}

// 约束参照视图
- (UIView *)contentWarp {
    if (_contentWarp == nil) {
        _contentWarp = [[UIView alloc] init];
        _contentWarp.backgroundColor = KWhiteColor;
    }
    return _contentWarp;
}

@end

